import 'package:fluro_decorate/fluro_decorate.dart';
import 'package:flutter/material.dart';

///
/// @ProjectName:    fluro_decorate
/// @Package:
/// @ClassName:      navigator_util
/// @Description:    路由跳转工具类
/// @Author:         WTT
/// @CreateDate:     2021/10/27 4:39 下午
class NavigatorUtil {
  /// 生成路由列表:
  /// onGenerateRoute: Application.router.generator,
  static final router = GlobalRouter();

  /// 是否展示日志栈打印
  static bool logStack = false;

  /// 需要记录的route栈
  static List _routeCacheStack = <String>[];

  /// 配置路由
  static configretuin(List<RouterRegister> routers,
      {bool showLogStack = false}) {
    logStack = showLogStack;
    routers.forEach((router) {
      if (router != null) {
        router.initRouters();
      }
    });
  }

  /// context
  /// [routePage] 跳转页面
  /// [bundle] Bundle主要用于传递数据；它保存的数据，是以key-value(键值对)的形式存在的。
  /// [cacheCurrent] 缓存当前页面用于操作完成返回当前页面。
  static Future<Object> pushNamed(BuildContext context, String routePage,
      {Bundle bundle, cacheCurrent = false}) {
    if (bundle == null) {
      bundle = Bundle();
    }
    bundle.setRoutePage(routePage);
    return push(context, bundle: bundle, cacheCurrent: cacheCurrent);
  }

  /// context
  /// [routePage] 跳转页面
  /// [bundle] Bundle主要用于传递数据；它保存的数据，是以key-value(键值对)的形式存在的。
  /// [cacheCurrent] 缓存当前页面用于操作完成返回当前页面。
  static Future<Object> push(BuildContext context,
      {@required Bundle bundle, cacheCurrent = false}) {
    if (bundle == null) {
      throw Exception(["Bundle is null."]);
    }

    if (!bundle.containsKey("routePage")) {
      throw Exception(["routePage is null."]);
    }

    String routePage = bundle.getString("routePage");

    if (routePage.isEmpty) {
      throw Exception(["routePage is isEmpty."]);
    }

    if (cacheCurrent) {
      _routeCacheStack.add(ModalRoute.of(context).settings.name);
    }
    return Navigator.pushNamed(context, routePage, arguments: bundle);
  }

  /// pop页面
  /// context
  /// [bundle] Bundle主要用于传递数据；它保存的数据，是以key-value(键值对)的形式存在的。
  static pop(BuildContext context, {Bundle bundle}) {
    FocusScope.of(context).unfocus();
    if (Navigator.canPop(context)) {
      Navigator.pop(context, bundle);
    }
  }

  /// 回到最后一个记录页面
  static toLastRoute(BuildContext context) {
    FocusScope.of(context).unfocus();
    if (_routeCacheStack.length > 0) {
      printLog("回到最后一个记录页面：$_routeCacheStack");
      String lastRoutePage = _routeCacheStack.removeLast();
      toLastRouteByPageName(context, lastRoutePage);
      return true;
    }
    return false;
  }

  /// 回到指定页面
  /// [routePage] 要返回的页面路由名称
  static toLastRouteByPageName(BuildContext context, String routePage) {
    FocusScope.of(context).unfocus();
    printLog("回到指定页面：$routePage :$_routeCacheStack");
    Navigator.popUntil(context, ModalRoute.withName(routePage));
  }

  /// 检查关闭的页面是否是记录页面，如果是，移除掉
  /// [routePage] 带检测route名称
  static void checkRouteCacheStack(String routePage) {
    printLog("检查关闭的页面是否是记录页面：$routePage ：$_routeCacheStack");
    if (_routeCacheStack.length > 0 && _routeCacheStack.last == routePage) {
      return _routeCacheStack.removeLast();
    }
    return null;
  }

  /// 跳转至指定页面并清空栈
  /// [routePage] 要跳转的页面
  static Future<Object> clearStackAndToRoute(
      BuildContext context, String routePage,
      {Bundle bundle}) {
    FocusScope.of(context).unfocus();
    _routeCacheStack.clear();
    RouteSettings routeSettings =
        RouteSettings(arguments: bundle, name: routePage);
    printLog("clearStackAndToRoute routeSettings:$routeSettings");
    return router.navigateTo(context, routePage,
        clearStack: true, routeSettings: routeSettings);
  }

  /// 打印日志
  /// [obj] 日志内容
  static void printLog(Object obj) {
    bool showLog = false;
    assert(showLog = true);
    if (showLog) {
      print("$obj\n ${logStack ? StackTrace.current : ""}");
    }
  }
}
